<template>
  <div class="white_div">
    <p>操作管理员</p>
    <!-- 增加用户 -->
    <el-card>
      <el-button type="success" @click="admin" icon="el-icon-circle-plus-outline">增加用户</el-button>

      <el-dialog title="增加用户" :visible.sync="addDialogVisible" width="28vw">
        <el-form label-width="auto">
          <el-form-item label="电话:" width="22vw">
            <el-input v-model="addadmin.userTel" placeholder="请输入电话" width="20vw"></el-input>
          </el-form-item>
          <el-form-item label="靶场积分:">
            <el-input v-model="addadmin.userjifen1" placeholder="请输入靶场积分"></el-input>
          </el-form-item>
          <el-form-item label="ctf积分:">
            <el-input v-model="addadmin.userJifen2" placeholder="请输入ctf积分"></el-input>
          </el-form-item>
          <el-form-item label="注册时间:">
            <el-input v-model="addadmin.userTime" placeholder="请输入注册时间" type="date"></el-input>
          </el-form-item>
          <el-form-item label="注册地:">
            <el-input v-model="addadmin.userLocation" placeholder="请输入注册地"></el-input>
          </el-form-item>
          <el-form-item label="昵称:">
            <el-input v-model="addadmin.userName" placeholder="请输入昵称"></el-input>
          </el-form-item>
          <el-form-item label="资产:">
            <el-input v-model="addadmin.userProperty" placeholder="请输入资产"></el-input>
          </el-form-item>
          <el-form-item label="状态:">
            <el-select style="float: left" v-model="addadmin.userStatus">
              <el-option value="0" label="正常"></el-option>
              <el-option value="1" label="冻结"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户类型:">
            <el-select style="float: left" v-model="addadmin.userType">
              <el-option value="0" label="学生"></el-option>
              <el-option value="1" label="讲师"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="VIP">
            <el-select style="float: left" v-model="addadmin.userVip">
              <el-option value="0" label="普通"></el-option>
              <el-option value="1" label="会员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别:">
            <el-select style="float: left" v-model="addadmin.userSex">
              <el-option value="男" label="男"></el-option>
              <el-option value="女" label="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload class="upload-demo" action="http://172.16.13.212:8090/uploadoss" :on-remove="handleRemove"
              :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <el-button size="small" type="primary" class="push">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
            <el-image :src="addadmin.userSrc" alt=""></el-image>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addadminInfo">确定</el-button>
        </span>
      </el-dialog>
      <!-- 模糊查询 -->
      <el-input placeholder="请输入内容" v-model="value" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="select"></el-button>
      </el-input>
      <!-- 表格渲染 -->
      <el-table :data="adminlist" border class="admin_table">
        <!-- <el-table-column prop="userId" label="id" width="100"> </el-table-column> -->
        <el-table-column type="index" label="id" width="120"> </el-table-column>
        <!-- <el-table-column prop="userType" label="类型" width="100">
  </el-table-column> -->
        <el-table-column prop="userSrc" width="150" label="头像">
          <template slot-scope="scope">
            <el-avatar :size="50" :src="scope.row.userSrc"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="userName" label="名字" width="150">
        </el-table-column>
        <el-table-column prop="userJifen1" label="积分1" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.userJifen1 == null">暂无</span>
            <span v-if="scope.row.userJifen1 != null">{{ scope.row.userJifen1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userJifen2" label="积分2" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.userJifen2 == null">暂无</span>
            <span v-if="scope.row.userJifen2 != null">{{ scope.row.userJifen2 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="userVip" label="身份" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.userVip == 0">普通用户</span>
            <span v-if="scope.row.userVip == 1">VIP</span>
          </template>
        </el-table-column>
        <el-table-column prop="userStatus" label="状态" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.userStatus == 0">冻结</span>
            <span v-if="scope.row.userStatus == 1">正常</span>
          </template>
        </el-table-column>
        <el-table-column prop="userProperty" label="资产" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.userProperty == null">暂无</span>
            <span v-if="scope.row.userProperty != null">{{ scope.row.userProperty }}云币</span>
          </template>
        </el-table-column>
        <el-table-column prop="roleId" label="用户权限" width="150">
          <template slot-scope="scope">
            <span v-if="scope.row.roleId == 3">管理员</span>
            <span v-if="scope.row.roleId == 4">超级管理员</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="updateadmininfo(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete"
              @click="deleteadmin(scope.row.userId)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 修改的模态框 -->
      <el-dialog title="修改管理员信息" :visible.sync="updateDialogVisible" width="600px">
        <el-form label-width="auto">
          <el-form-item label="编号：">
            <el-input v-model="updateadmin.userId" placeholder="请输入编号" readonly></el-input>
          </el-form-item>
          <el-form-item label="电话：">
            <el-input v-model="updateadmin.userTel" placeholder="请输入电话"></el-input>
          </el-form-item>
          <el-form-item label="靶场积分：">
            <el-input v-model="updateadmin.userjifen1" placeholder="请输入靶场积分"></el-input>
          </el-form-item>
          <el-form-item label="ctf积分：">
            <el-input v-model="updateadmin.userJifen2" placeholder="请输入ctf积分"></el-input>
          </el-form-item>
          <el-form-item label="注册时间：">
            <el-input v-model="updateadmin.userTime" placeholder="请输入注册时间"></el-input>
          </el-form-item>
          <el-form-item label="注册地：">
            <el-input v-model="updateadmin.userLocation" placeholder="请输入注册地"></el-input>
          </el-form-item>
          <el-form-item label="昵称：">
            <el-input v-model="updateadmin.userName" placeholder="请输入昵称"></el-input>
          </el-form-item>
          <el-form-item label="资产：">
            <el-input v-model="updateadmin.userProperty" placeholder="请输入资产"></el-input>
          </el-form-item>
          <el-form-item label="状态：">
            <el-select style="float: left" v-model="updateadmin.userStatus">
              <el-option value="0" label="正常"></el-option>
              <el-option value="1" label="冻结"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户类型：">
            <el-select style="float: left" v-model="updateadmin.userType">
              <el-option value="0" label="学生"></el-option>
              <el-option value="1" label="讲师"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="VIP：">
            <el-select style="float: left" v-model="updateadmin.userVip">
              <el-option value="0" label="普通"></el-option>
              <el-option value="1" label="会员"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="性别：">
            <el-select style="float: left" v-model="updateadmin.userSex">
              <el-option value="男" label="男"></el-option>
              <el-option value="女" label="女"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="头像：">
            <el-upload style="float: left" class="avatar-uploader" action="http://172.16.13.212:8090/uploadoss"
              :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
              <img v-if="updateadmin.userSrc" :src="updateadmin.userSrc" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updatePersonApi">确定</el-button>
        </span>
      </el-dialog>
    </el-card>

  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapMutations } = createNamespacedHelpers("tags");
export default {
  data() {
    return {
      updateDialogVisible: false,
      addDialogVisible: false,
      adminlist: [],
      value: "",
      info: {
        userName: "",
        currentPage: 1,
        pageSize: 5,
      },
      query: {
        currentPage: 1,
        pageSize: 5,
      },
      //修改编辑
      updateadmin: {
        userId: "",
        userTel: "",
        userjifen1: "",
        userJifen2: "",
        userTime: "",
        userLocation: "",
        userName: "",
        userPaw: "",
        userProperty: "",
        userStatus: "",
        userType: "",
        userSex: "",
        userSrc: "",
      },
      //增加信息
      addadmin: {
        // userId: "",
        userTel: "",
        userjifen1: "",
        userJifen2: "",
        userTime: "",
        userLocation: "",
        userName: "",
        userPaw: "",
        userProperty: "",
        userStatus: "",
        userType: "",
        userSex: "",
        userSrc: "",
      },
    };
  },
  methods: {
    ...mapMutations(["addTabs"]),
    async select() {
      console.log(this.value);
      this.info.userName = this.value
      const res = await this.$api.admin.namelike(this.info)
      console.log(res);
      if (res.code) {
        this.adminlist = res.data.userList2
        console.log(this.adminlist);
        this.$message.success(res.message);
      }
    },
    async getadmin() {
      console.log(111111);
      console.log(this.query);
      const res = await this.$api.admin.getalladmin(this.query);
      console.log(res);
      this.adminlist = res.data.UserAdministratorList;
    },
    async addadminInfo() {
      console.log(this.addadmin);
      const res = await this.$api.admin.addAdmin(this.addadmin);
      console.log('成功添加管理员', res);
      if (res.code) {
        this.addDialogVisible = false
        this.$message.success(res.message);
        this.getadmin();
      }
    },
    admin() {
      this.addDialogVisible = true;
    },
    async deleteadmin(id) {
      console.log(id);
      const res = await this.$api.admin.deladmin(id);
      console.log(res);
      if (res.code) {
        this.addDialogVisible = false;
        this.$message.success(res.message);
        this.getadmin();
      }
    },
    updateadmininfo(info) {
      console.log(info);
      this.updateadmin = info;
      this.updateDialogVisible = true;
    },
    async updatePersonApi() {
      console.log(this.updateadmin);
      const res = await this.$api.admin.updateadmin(this.updateadmin);
      console.log(res);
      if (res.code) {
        this.updateDialogVisible = false;
        this.$message.success(res.message);
        this.getadmin();
      }
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handleAvatarSuccess(val) {
      console.log("图片", val.message);
      this.addadmin.userSrc = val.message;
      this.updateadmin.userSrc = val.message;
    },
    beforeAvatarUpload(file) {
      const isJPGaPNG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPGaPNG) {
        this.$message.error("上传头像图片只能是 JPG或PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      console.log("aaaaa", isLt2M);
      return isLt2M && isJPGaPNG;
    },
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.addTabs(to)
    })
  },
  created() {
    this.getadmin();
  },
};
</script>

<style scoped>
.admin_table {
  margin-top: 2vw;
}

.input-with-select {
  width: 36vw;
  margin-left: 5vw;
}

.white_div {
  background-color: white;
  padding: 2.6042vw 0;
}

.avatar-uploader ::v-deep .el-upload {
  border: 0.052vw dashed #d9d9d9;
  border-radius: 0.313vw;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 1.458vw;
  color: #8c939d;
  width: 9.271vw;
  height: 9.271vw;
  line-height: 9.271vw;
  text-align: center;
}

.avatar {
  width: 9.271vw;
  height: 9.271vw;
  display: block;
}
</style>